<?php
// +----------------------------------------------------------------------
// | ZanPianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpiancms.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.zanpiancms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use app\common\library\Letter;
use think\Cache;
class Topic extends Model{
	//关联方法
	public function vod(){
        return $this->hasMany('vod','vod_id','topic_tid')->where('vod_status',1);
    }
	public function news(){
        return $this->hasMany('news','news_id','topic_tid')->where('news_status',1);
    }
	public function star(){
        return $this->hasMany('news','news_tid','topic_tid')->where('news_status',1);
    }
	public function topic_del($did='',$sid='',$tid='',$tsid=''){
		if(!empty($did)){
			$where['topic_did']=$did;
		}		
		if(!empty($sid)){
			$where['topic_sid']=$sid;
		}
		if(!empty($tid)){
			$where['topic_tid']=$tid;
		}
		if(!empty($tsid)){
			$where['topic_tsid']=$tsid;
		}			
		if(!empty($where)){
		   db('topic')->where($where)->delete();	
		}	
	}
	public function topic_updata($tid='',$sid='',$keyword=''){
		if(is_array($keyword)){
		    $keyarray = $keyword;
		}else{
			$keyarray = explode(',',str_replace(array('，','、'),',',$keyword));
		}
		$keyarray=array_filter(array_unique($keyarray));
        if($sid==1){
			foreach($keyarray as $key=>$val){
                if(is_numeric($val)){
				   $data[$key]['topic_did'] = $val;
                   $data[$key]['topic_name'] = '';				   
				   
			    }else{
				    $vod=db('vod')->where('vod_name',$val)->field('vod_name,vod_id')->order('vod_addtime desc')->find();
				    if(!empty($vod['vod_id'])){
					    $data[$key]['topic_did'] = $vod['vod_id'];
                        $data[$key]['topic_name'] = '';						
					   
				    }else{
						$data[$key]['topic_did'] = 0;
						$data[$key]['topic_name'] = $val;
					}
					
				}
				$data[$key]['topic_sid']=$sid;
		        $data[$key]['topic_tid']=$tid;
				$data[$key]['topic_tsid']=2;
				$data[$key]['topic_oid']=$key+1;
				$data[$key]['topic_addtime']=time()+$key;
			}
			db('topic')->where(array('topic_tid'=>$tid,'topic_sid'=>$sid,'topic_tsid'=>2))->delete();
		    if(isset($data)){
		        db('topic')->insertAll($data);   
		    }
            unset($data);			
		}
        elseif($sid==3){
			foreach($keyarray as $key=>$val){
                if(is_numeric($val)){
				   $data[$key]['topic_did'] = $val; 
                   $data[$key]['topic_name'] = '';				   
				   
			    }else{
					$star=db('star')->where('star_name',$val)->field('star_name,star_id')->order('star_addtime desc')->find();
				    if(!empty($star['star_id'])){
					    $data[$key]['topic_did'] = $star['star_id']; 
                        $data[$key]['topic_name'] = '';						
					   
				    }else{
						$data[$key]['topic_did'] = 0;
						$data[$key]['topic_name'] = $val;
					}
					
				}				
				$data[$key]['topic_sid']=$sid;
		        $data[$key]['topic_tid']=$tid;
				$data[$key]['topic_tsid']=2;
				$data[$key]['topic_oid']=$key+1;
				$data[$key]['topic_addtime']=time()+$key;
			}
			//print_r($data);
			db('topic')->where(array('topic_tid'=>$tid,'topic_sid'=>$sid,'topic_tsid'=>2))->delete();
		    if(isset($data)){
		       db('topic')->insertAll($data);   
		    }
            unset($data);			
		}else{
			foreach($keyarray as $key=>$val){
				if(!is_numeric($val)){
			        $vod=db('vod')->where('vod_name',$val)->field('vod_name,vod_id')->order('vod_addtime desc')->find();
			        $star=db('star')->where('star_name',$val)->field('star_name,star_id')->order('star_addtime desc')->find();						
			        if(!empty($vod['vod_id'])){
			            $data[$key]['topic_did'] = $vod['vod_id'];
					    $data[$key]['topic_sid'] = 1;
						$data[$key]['topic_name'] = $vod['vod_name'];
			        }
					elseif(!empty($star['star_id'])){
			            $data[$key]['topic_did'] = $star['star_id'];
					    $data[$key]['topic_sid'] = 3;
						$data[$key]['topic_name'] = $star['star_name'];
			        }else{
			            $data[$key]['topic_did'] = 0;	
					    $data[$key]['topic_sid'] = 0;
                        $data[$key]['topic_name']=$val;						
			        }					
				}
				if(empty($data[$key]['topic_did']) && empty($data[$key]['topic_name'])){
					continue;
				}
				$data[$key]['topic_tid']=$tid;
				$data[$key]['topic_tsid']=2;
				$data[$key]['topic_oid']=$key+1;
				$data[$key]['topic_addtime']=time()+$key;
				db('topic')->where($data[$key])->delete();
			}
		    if(isset($data)){
		        db('topic')->insertAll($data);   
		    }
            unset($data);			
			
			
		}		
		
   }
    public function listData($tag){
		$param = zanpiancms_param_url();
		if(!is_array($tag)) {
            $tag=json_decode($tag, true);
        }
		$tag['limit']= !empty($tag['limit']) ? $tag['limit'] : 10;	
		$cahce=tag_cache($tag);
	    //优先从缓存调用数据及分页变量
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){			
			$data_cache_content = Cache::get($cahce['cache_name']);
			if($data_cache_content){
				return $data_cache_content;
			}
		}		
		$where=[];
		//$where['story_status']=['eq',1];
		//$where['actor_status']=['eq',1];
	    if(!empty($tag['tid'])){
			    $where['topic_tid'] = array('eq',$tag['tid']);
	    }		
	    if(!empty($tag['sid'])){
			    $where['topic_sid'] = array('eq',$tag['sid']);
	    }
	    if(!empty($tag['did'])){
			    $where['topic_did'] = array('eq',$tag['did']);
	    }
	    if(!empty($tag['tsid'])){
			    $where['topic_tsid'] = array('eq',$tag['tsid']);
	    }
        if(!empty($tag['cid'])) {
		    $cids = explode(',',trim($tag['cid']));
		    if(count($cids) > 1) {
			    $where['news_cid'] = array('in',getlistarr_tag($cids));
		    }else{
				$where['news_cid'] = getlistsqlin($tag['cid']);
		    }
	    }		
		if(!empty($tag['tid']) && !empty($tag['tsid']) && $tag['tsid']==2 && $tag['sid']==2){
			unset($where['topic_sid']);
			$list=db('topic')->field('topic_did,topic_name')->where($where)->select();
			foreach($list as $key=>$val){
				if(!empty($val['topic_did'])){
				   $list_id[]=$val['topic_did'];	
				}else{
				   $list_name[]=$val['topic_name'];
				}
			}
			if(!empty($list_id)){
				$wheres['news_id']=['neq',$tag['tid']];
				$wheres['topic_did']=['in',$list_id];
			}
			if(!empty($list_name)){
				$GLOBALS['where']['news_id']=['neq',$tag['tid']];
				$GLOBALS['where']['topic_name']=['in',$list_name];
			}
			if (!empty($tag['play'])){
			    if($tag['play']==1){
		            $where['news_playurl'] = array('neq','');
			    }else{
				    $where['news_playurl'] = array('eq','');
			    }
	        }
			if(!empty($list_id) || !empty($list_name)){
                $limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));			
		        $data=db('topic')->join('news news','news.news_id=topic_tid','RIGHT')->where($wheres)->whereOr(function($query){
                    $query->where($GLOBALS['where']);
                })->field($tag['field'])->order($tag['order'])->group('topic_tid')->limit($limit_str)->order('news_addtime desc')->select();
			}
		}elseif(!empty($tag['tid']) && !empty($tag['tsid']) && $tag['tsid']==1 && $tag['sid']==1){
			$GLOBALS['where']['topic_did']=['eq',$tag['tid']];
			$GLOBALS['where']['topic_sid']=['eq',1];
			$GLOBALS['where']['topic_tsid']=['eq',1];
			$GLOBALS['wheres']['topic_tid']=['eq',$tag['tid']];
			$GLOBALS['wheres']['topic_sid']=['eq',1];
			$GLOBALS['wheres']['topic_tsid']=['eq',1];
            $limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));	
            $list=db('topic')->where($GLOBALS['where'])->whereOr('topic_did','in',function($query){
                    $query->table(config('database.prefix').'topic')->where($GLOBALS['wheres'])->field('topic_did');
            })->select();
			$vodid=[];
			if($list){
			    foreach($list as $key=>$val){
				   $topic_did[]=$val['topic_did'];	
				   $topic_tid[]=$val['topic_tid'];
			    }
				$vodid=array_unique(array_merge($topic_did,$topic_tid));
			    unset($where);
			    $where['vod_id']=['in',$vodid];
                $where['vod_status']=['eq',1];			
			    $data=db('vod')->where($where)->where('vod_id','neq',$tag['tid'])->limit($limit_str)->select();				
		    }
		}
        elseif(!empty($tag['tid']) && !empty($tag['tsid']) && $tag['sid']==1){
			$tag['field']= !empty($tag['field']) ? $tag['field'] : 'vod_id,vod_cid,vod_mcid,vod_name,vod_aliases,vod_title,vod_color,vod_actor,vod_director,vod_content,vod_pic,vod_bigpic,vod_diantai,vod_tvcont,vod_tvexp,vod_area,vod_language,vod_year,vod_continu,vod_total,vod_isend,vod_addtime,vod_hits,vod_hits_day,vod_hits_week,vod_hits_month,vod_hits_lasttime,vod_stars,vod_up,vod_down,vod_play,vod_url,vod_gold,vod_golder,vod_isfilm,vod_filmtime,vod_length,vod_weekday,vod_letters,vod_bigpic,vod_label,vod_jumpurl';		
		    if(empty(order_check($tag['order']))){
			    $tag['order']='topic_oid desc';
		    }
			if(!empty($tag['page'])){
            $url=urldecode(zanpian_list_url($GLOBALS['route'],config('params')));
		    $data=db('topic')->join('vod vod','vod.vod_id=topic_did')->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		    }else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data=db('topic')->join('vod vod','vod.vod_id=topic_did')->where($where)->field($tag['field'])->order($tag['order'])->limit($limit_str)->select();
		    }
	    }elseif(!empty($tag['tid']) && !empty($tag['tsid']) && $tag['sid']==3){
			$tag['field']= !empty($tag['field']) ? $tag['field'] : 'star_id,star_cid,star_name,star_bm,star_mcid,star_prty,star_wwm,star_letters,star_tag,star_letter,star_color,star_bgcolor,star_pig,star_pic,star_bigpic,star_xb,star_sg,star_tz,star_mz,star_cstime,star_school,star_gs,star_weibo,star_work,star_guanxi,star_zy,star_xz,star_xx,star_area,star_gj,star_csd,star_content,star_reurl,star_skin,star_jumpurl,star_hits,star_hits_day,star_hits_week,star_hits_month,star_hits_lasttime,star_addtime,star_stars,star_status,star_up,star_down,star_label';		
		    if(empty(order_check($tag['order']))){
			    $tag['order']='topic_oid desc';
		    }
			if(!empty($tag['page'])){
            $url=urldecode(zanpian_list_url($GLOBALS['route'],config('params')));
		    $data = db('topic')->join('star star','star.star_id=topic_did')->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		    }else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data=db('topic')->join('star star','star.star_id=topic_did')->where($where)->field($tag['field'])->order($tag['order'])->limit($limit_str)->select();
		    }			
		}elseif(!empty($tag['tid']) && !empty($tag['tsid']) && $tag['sid']==2){
			$tag['field']= !empty($tag['field']) ? $tag['field'] : 'news_id,news_cid,news_name,news_mid,news_prty,news_tag,news_color,news_pic,news_bigpic,news_remark,news_playname,news_playurl,news_hits,news_hits_day,news_hits_week,news_hits_month,news_hits_lasttime,news_stars,news_status,news_up,news_down,news_jumpurl,news_letter,news_addtime,news_skin,news_gold,news_golder,news_playtime,news_clarity';		
			if (!empty($tag['play'])){
			    if($tag['play']==1){
		            $where['news_playurl'] = array('neq','');
			    }else{
				    $where['news_playurl'] = array('eq','');
			    }
	        }		    
			if(empty(order_check($tag['order']))){
			    $tag['order']='news_addtime desc';
		    }
			if(!empty($tag['page'])){
            $url=urldecode(zanpian_list_url($GLOBALS['route'],config('params')));
		    $data = db('topic')->join('news news','news.news_id=topic_did')->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		    }else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data=db('topic')->join('news news','news.news_id=topic_did')->where($where)->field($tag['field'])->order($tag['order'])->limit($limit_str)->select();
		    }			
		}elseif(!empty($tag['sid']) && !empty($tag['did']) && $tag['tsid']==2){
			$tag['field']= !empty($tag['field']) ? $tag['field'] : 'news_id,news_cid,news_name,news_mid,news_prty,news_tag,news_color,news_pic,news_bigpic,news_remark,news_playname,news_playurl,news_hits,news_hits_day,news_hits_week,news_hits_month,news_hits_lasttime,news_stars,news_status,news_up,news_down,news_jumpurl,news_letter,news_addtime,news_skin,news_gold,news_golder,news_playtime,news_clarity';		
		    if(empty(order_check($tag['order']))){
			    $tag['order']='news_addtime desc';
		    }
			if (!empty($tag['play'])){
			    if($tag['play']==1){
		            $where['news_playurl'] = array('neq','');
			    }else{
				    $where['news_playurl'] = array('eq','');
			    }
	        }			
			if(!empty($tag['page'])){
            $url=urldecode(zanpian_list_url($GLOBALS['route'],config('params')));
		    $data = db('topic')->join('news news','news.news_id=topic_tid')->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		    }else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data=db('topic')->join('news news','news.news_id=topic_tid')->where($where)->field($tag['field'])->order($tag['order'])->limit($limit_str)->select();
		    }			
		}elseif(!empty($tag['sid']) && !empty($tag['did']) && $tag['tsid']==10){
			$tag['field']= !empty($tag['field']) ? $tag['field'] : '*';			
		    if(empty(order_check($tag['order']))){
			    $tag['order']='topic_oid desc';
		    }
			if(!empty($tag['page'])){
            $url=urldecode(zanpian_list_url($GLOBALS['route'],config('params')));
		    $data=db('topic')->join('special special','special.special_id=topic_tid')->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		    }else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data=db('topic')->join('special special','special.special_id=topic_tid')->where($where)->field($tag['field'])->order($tag['order'])->limit($limit_str)->select();
		    }
		}elseif(empty($tag['model']) && empty($tag['tid']) && !empty($tag['tsid']) && $tag['sid']==3){
			$tag['field']= !empty($tag['field']) ? $tag['field'] : 'star_id,star_cid,star_name,star_bm,star_mcid,star_prty,star_wwm,star_letters,star_tag,star_letter,star_color,star_bgcolor,star_pig,star_pic,star_bigpic,star_xb,star_sg,star_tz,star_mz,star_cstime,star_school,star_gs,star_weibo,star_work,star_guanxi,star_zy,star_xz,star_xx,star_area,star_gj,star_csd,star_content,star_reurl,star_skin,star_jumpurl,star_hits,star_hits_day,star_hits_week,star_hits_month,star_hits_lasttime,star_addtime,star_stars,star_status,star_up,star_down';		
		    if(empty(order_check($tag['order']))){
			    $tag['order']='topic_oid desc';
		    }
			if(!empty($tag['page'])){
            $url=urldecode(zanpian_list_url($GLOBALS['route'],config('params')));
		    $data = db('topic')->join('star star','star.star_id=topic_did')->where($where)->field($tag['field'])->group($tag['group'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		    }else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data=db('topic')->join('star star','star.star_id=topic_did')->where($where)->field($tag['field'])->group($tag['group'])->order($tag['order'])->limit($limit_str)->select();
		    }			
		}elseif(empty($tag['model']) && empty($tag['tid']) && !empty($tag['tsid']) && $tag['sid']==1){
			$tag['field']= !empty($tag['field']) ? $tag['field'] : 'vod_id,vod_cid,vod_mcid,vod_name,vod_aliases,vod_title,vod_color,vod_actor,vod_director,vod_content,vod_pic,vod_bigpic,vod_diantai,vod_tvcont,vod_tvexp,vod_area,vod_language,vod_year,vod_continu,vod_total,vod_isend,vod_addtime,vod_hits,vod_hits_day,vod_hits_week,vod_hits_month,vod_hits_lasttime,vod_stars,vod_up,vod_down,vod_play,vod_url,vod_gold,vod_golder,vod_isfilm,vod_filmtime,vod_length,vod_weekday,vod_letters,vod_bigpic,vod_label,vod_jumpurl';		
		    if(empty(order_check($tag['order']))){
			    $tag['order']='topic_oid desc';
		    }
			if(!empty($tag['page'])){
            $url=urldecode(zanpian_list_url($GLOBALS['route'],config('params')));
		    $data=db('topic')->join('vod vod','vod.vod_id=topic_did')->where($where)->group($tag['group'])->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		    }else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data=db('topic')->join('vod vod','vod.vod_id=topic_did')->where($where)->group($tag['group'])->field($tag['field'])->order($tag['order'])->limit($limit_str)->select();
		    }
	    }elseif(!empty($tag['sid']) && !empty($tag['tsid']) && !empty($tag['model'])){
			if($tag['model']==1){
			    $tag['field']= !empty($tag['field']) ? $tag['field'] : 'vod_id,vod_cid,vod_mcid,vod_name,vod_aliases,vod_title,vod_color,vod_actor,vod_director,vod_content,vod_pic,vod_bigpic,vod_diantai,vod_tvcont,vod_tvexp,vod_area,vod_language,vod_year,vod_continu,vod_total,vod_isend,vod_addtime,vod_hits,vod_hits_day,vod_hits_week,vod_hits_month,vod_hits_lasttime,vod_stars,vod_up,vod_down,vod_play,vod_url,vod_gold,vod_golder,vod_isfilm,vod_filmtime,vod_length,vod_weekday,vod_letters,vod_bigpic,vod_label,vod_jumpurl';		
		        if(empty(order_check($tag['order']))){
			        $tag['order']='topic_oid desc';
		        }
			    if(!empty($tag['page'])){
                    $url=urldecode(zanpian_list_url($GLOBALS['route'],config('params')));
		            $data=db('topic')->join('vod vod','vod.vod_id=topic_did')->where($where)->group($tag['group'])->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		        }else{
			        $limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			        $data=db('topic')->join('vod vod','vod.vod_id=topic_did')->where($where)->group($tag['group'])->field($tag['field'])->order($tag['order'])->limit($limit_str)->select();
		        }				
			}elseif($tag['model']==2){
			    $tag['field']= !empty($tag['field']) ? $tag['field'] : 'news_id,news_cid,news_name,news_mid,news_prty,news_tag,news_color,news_pic,news_bigpic,news_remark,news_playname,news_playurl,news_hits,news_hits_day,news_hits_week,news_hits_month,news_hits_lasttime,news_stars,news_status,news_up,news_down,news_jumpurl,news_letter,news_addtime,news_skin,news_gold,news_golder';		
		        if(empty(order_check($tag['order']))){
			        $tag['order']='topic_oid desc';
		        }
			    if (!empty($tag['play'])){
			        if($tag['play']==1){
		                $where['news_playurl'] = array('neq','');
			        }else{
				        $where['news_playurl'] = array('eq','');
			        }
	            }			
			    if(!empty($tag['page'])){
                    $url=urldecode(zanpian_list_url($GLOBALS['route'],config('params')));
		            $data = db('topic')->join('news news','news.news_id=topic_tid')->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		        }else{
			        $limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			        $data=db('topic')->join('news news','news.news_id=topic_tid')->where($where)->field($tag['field'])->order($tag['order'])->limit($limit_str)->select();
		        }				
								
			}			
		}
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){
			cache::tag('foreach_vod')->set($cahce['cache_name'],$data,intval($cahce['cache_time'])); 
	    }
		return $data;
    }    

   
	
}
// +----------------------------------------------------------------------
// | ZanPianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpiancms.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.zanpiancms.cc>
// +----------------------------------------------------------------------